# Maintainer: Jelle van der Waa <jelle@archlinux.org>

pkgname=keycloak-metrics-spi
pkgver=3.0.0
pkgrel=2
pkgdesc='A Service Provider that adds a Prometheus metrics endpoint to Keycloak'
url="https://github.com/aerogear/keycloak-metrics-spi"
arch=('any')
license=('Apache')
_jrever=11
_jdkver=11
depends=("java-runtime>=${_jrever}")
makedepends=("jdk${_jdkver}-openjdk" 'gradle' 'keycloak')
source=($pkgname-$pkgver.tar.gz::https://github.com/aerogear/keycloak-metrics-spi/archive/${pkgver}.tar.gz
        move-version-to-properties.patch
        reproducible-build.patch
        remove-manifest-variable-fields.patch)
sha512sums=('2fdb15cc44f3325e47ae31bcc8a861556fd399d66a48771744820ec1e614380fef2fc8596644288b32093a3e17be7dcf84325403e2663419ff457e51d1a2a12a'
            '6b4190835f6d0cb85d57a3aa50c0c87eae1bcf4865797b3208ecf85faa6bd35b483db73dd2f34e8479fa851dbf6595cecbf97fc72cc8d1a2df1e2fdfbdf6209e'
            '238cd8e201d2831686890eacc4c0188e2f56c93212d66a367a37921408ad768a463aa088472140bc5701f70e0d32c637c75cc18a1bb4b3d2d9d14654888a70f7'
            '356507ba6f056ea33522d7e3e297fd17df84eb8243d9802a8b902dd59808ea1237f2d138487d5061a68d849e289daf5c4e7738ee6facb28987a6df20def83fce')

prepare() {
  cd ${pkgname}-${pkgver}
  # https://github.com/aerogear/keycloak-metrics-spi/pull/164
  patch -Np1 < "${srcdir}/move-version-to-properties.patch"
  # https://github.com/aerogear/keycloak-metrics-spi/pull/165
  patch -Np1 < "${srcdir}/reproducible-build.patch"
  patch -Np1 < "${srcdir}/remove-manifest-variable-fields.patch"
}

build() {
  cd ${pkgname}-${pkgver}
  export JAVA_HOME="/usr/lib/jvm/java-${_jdkver}-openjdk"
  export PATH="/usr/lib/jvm/java-${_jdkver}-openjdk/bin:$PATH"
  gradle -Pversion=${pkgver} assemble
}

check() {
  cd ${pkgname}-${pkgver}
  export JAVA_HOME="/usr/lib/jvm/java-${_jdkver}-openjdk"
  export PATH="/usr/lib/jvm/java-${_jdkver}-openjdk/bin:$PATH"
  gradle -Pversion=${pkgver} test
}

package() {
  cd ${pkgname}-${pkgver}

  install -d "${pkgdir}/usr/share/java/keycloak/providers/"
  install -Dm644 "build/libs/keycloak-metrics-spi-${pkgver}.jar" "${pkgdir}/usr/share/java/${pkgname}/keycloak-metrics-spi-${pkgver}.jar"
  ln -s "/usr/share/java/${pkgname}/keycloak-metrics-spi-${pkgver}.jar" "${pkgdir}/usr/share/java/keycloak/providers/keycloak-metrics-spi-${pkgver}.jar"
}
